package com.tzbank.poc.controller;

import com.tzbank.poc.exception.ServiceException;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**Controller增强器
 * @author li_zheng2
 * Create by li_zheng2 on 2019/1/22.
 */
@ControllerAdvice
public class ServiceControllerAdvice {

//    @InitBinder
//    public void initBinder(WebDataBinder binder){}

//    @ModelAttribute
//    public void addAttributes(Model model){
//        model.addAttribute("author", "11");
//    }

    /**
     *全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Map errorHadler(Exception ex){
        Map map = new HashMap<>();
        map.put("RetCode","1000");
        map.put("RetStatus","F");
        map.put("RetMsg",ex.getMessage());
        return map;
    }

    @ResponseBody
    @ExceptionHandler(value = ServiceException.class)
    public Map commonErrorHandler(ServiceException ex){
        Map map = new HashMap();
        map.put("RetCode",ex.getCode());
        map.put("RetMsg",ex.getMsg());
        return map;
    }
}
